package com.nf.health.app.inquiry.adapter;


import android.content.Context;
import android.os.Bundle;
import android.text.Spannable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.nf.health.app.MyApplication;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.PictureActivity;
import com.nf.health.app.inquiry.bean.InquiryDetailBean;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.ImageLoaderUtils;
import com.nf.health.app.utils.JsonUtils;
import com.nf.health.app.utils.LogUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * 问诊界面显示聊天信息,这里只显示,不聊天
 */

public class MessageAdapter extends BaseAdapter {
    private static final int MESSAGE_TYPE_SENT_TXT = 1;//用户发送文字
    private static final int MESSAGE_TYPE_RECV_TXT = 2;//医生发送文字
    private static final int MESSAGE_TYPE_SENT_IMAGE = 3;//用户发送图片
    private static final int MESSAGE_TYPE_RECV_IMAGE = 4;//医生发送图片
    private Context context;
    private List<InquiryDetailBean.DoctorlistBean.InquiryreplylistBean> inquiryReplyList;


    /**
     * @param context
     */
    public MessageAdapter(Context context, List<InquiryDetailBean.DoctorlistBean.InquiryreplylistBean> inquiryReplyList) {
        this.context = context;
        this.inquiryReplyList = inquiryReplyList;
    }

    @Override
    public int getCount() {
        return inquiryReplyList == null ? 0 : inquiryReplyList.size();
    }

    @Override
    public InquiryDetailBean.DoctorlistBean.InquiryreplylistBean getItem(int position) {
        return inquiryReplyList.get(position);
    }

    @Override
    public long getItemId(int id) {
        return id;
    }


    @Override
    public int getViewTypeCount() {
        return 5;
    }

    @Override
    public int getItemViewType(int position) {
        String typestate = JsonUtils.getFiledData(getItem(position).getContent(), "type");
        if (typestate.equals("2")) {
            return MESSAGE_TYPE_RECV_TXT;
        } else {
            if (typestate.equals("1")) {
                return MESSAGE_TYPE_SENT_TXT;
            } else {
                if (typestate.equals("4")) {
                    return MESSAGE_TYPE_RECV_IMAGE;
                } else {
                    if (typestate.equals("3")) {
                        return MESSAGE_TYPE_SENT_IMAGE;
                    } else {
                        return 0;
                    }
                }
            }
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = createViewByType(position);
            switch (getItemViewType(position)) {
                case MESSAGE_TYPE_RECV_TXT:
                    holder.timestamp = (TextView) convertView.findViewById(R.id.timestamp);
                    holder.iv_userHead = (ImageView) convertView.findViewById(R.id.iv_userhead);
                    holder.chat_content = (TextView) convertView.findViewById(R.id.tv_chatcontent);
                    break;
                case MESSAGE_TYPE_SENT_TXT:
                    holder.timestamp = (TextView) convertView.findViewById(R.id.timestamp);
                    holder.iv_userHead = (ImageView) convertView.findViewById(R.id.iv_userhead);
                    holder.chat_content = (TextView) convertView.findViewById(R.id.tv_chatcontent);
                    break;
                case MESSAGE_TYPE_RECV_IMAGE:
                    holder.timestamp = (TextView) convertView.findViewById(R.id.timestamp);
                    holder.iv_userHead = (ImageView) convertView.findViewById(R.id.iv_userhead);
                    holder.iv_sendPicture = (ImageView) convertView.findViewById(R.id.iv_sendPicture);
                    break;
                case MESSAGE_TYPE_SENT_IMAGE:
                    holder.timestamp = (TextView) convertView.findViewById(R.id.timestamp);
                    holder.iv_userHead = (ImageView) convertView.findViewById(R.id.iv_userhead);
                    holder.iv_sendPicture = (ImageView) convertView.findViewById(R.id.iv_sendPicture);
                    break;
            }
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        final String message = JsonUtils.getFiledData(getItem(position).getContent(), "message");
        switch (getItemViewType(position)) {
            case MESSAGE_TYPE_RECV_TXT:
            case MESSAGE_TYPE_SENT_TXT:
                // 设置内容,加环信表情20170313
                Spannable span = EaseSmileUtils.getSmiledText(context, message);
                holder.chat_content.setText(span, TextView.BufferType.SPANNABLE);
                //设置内容
//                holder.chat_content.setText(message);
                break;

            case MESSAGE_TYPE_RECV_IMAGE:
                try {
                    ImageLoaderUtils.getinstance(context).getImage(holder.iv_sendPicture, message, R.drawable.default_image);
                    holder.iv_sendPicture.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ArrayList<String> imgUrls = new ArrayList<>();
                            imgUrls.add(message);
                            Bundle bundle = new Bundle();
                            bundle.putStringArrayList("imgUrls", imgUrls);
                            bundle.putInt("position", 0);
                            //接收到的图片肯定都是网络类型
                            if (!TextUtils.isEmpty(message) && message.startsWith("http://")) {
                                bundle.putInt("type", PictureActivity.TYPE_NET_IMG);
                            } else {
                                bundle.putInt("type", PictureActivity.TYPE_LOCAL_IMG);
                            }
                            ActivityUtils.showActivity(context, PictureActivity.class, bundle);
                        }
                    });
                } catch (Exception e) {
                    LogUtil.exception(e);
                }
                break;
            case MESSAGE_TYPE_SENT_IMAGE:
                try {
                    ImageLoader.getInstance().displayImage(message, holder.iv_sendPicture, MyApplication.optionsHead);

                    holder.iv_sendPicture.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ArrayList<String> imgUrls = new ArrayList<>();
                            imgUrls.add(message);
                            Bundle bundle = new Bundle();
                            bundle.putStringArrayList("imgUrls", imgUrls);
                            bundle.putInt("position", 0);
                            //接收到的图片肯定都是网络类型
                            if (!TextUtils.isEmpty(message) && message.startsWith("http://")) {
                                bundle.putInt("type", PictureActivity.TYPE_NET_IMG);
                            } else {
                                bundle.putInt("type", PictureActivity.TYPE_LOCAL_IMG);
                            }
                            ActivityUtils.showActivity(context, PictureActivity.class, bundle);
                        }
                    });
                } catch (Exception e) {
                    LogUtil.exception(e);
                }
                break;
        }

        //隐藏头像
        holder.iv_userHead.setVisibility(View.GONE);
        //设置时间
        if (position == 0) {
            holder.timestamp.setText(DateUtil.date2String(getItem(position).getCreatetime(), "yy-MM-dd HH:mm:ss"));
            holder.timestamp.setVisibility(View.VISIBLE);
        } else {
            //两条消息时间离得如果稍长，显示时间
            if (DateUtil.isCloseEnough(getItem(position).getCreatetime(), getItem(position - 1).getCreatetime())) {
                holder.timestamp.setVisibility(View.GONE);
            } else {
                holder.timestamp.setText(DateUtil.date2String(getItem(position).getCreatetime(), "yy-MM-dd HH:mm:ss"));
                holder.timestamp.setVisibility(View.VISIBLE);
            }
        }
        return convertView;
    }

    class ViewHolder {
        ImageView iv_userHead;
        ImageView iv_sendPicture;
        TextView chat_content;
        TextView timestamp;
    }

    private View createViewByType(int position) {
        switch (getItemViewType(position)) {
            case MESSAGE_TYPE_RECV_TXT:
                return View.inflate(context, R.layout.row_received_message, null);
            case MESSAGE_TYPE_SENT_TXT:
                return View.inflate(context, R.layout.row_sent_message, null);
            case MESSAGE_TYPE_RECV_IMAGE:
                return View.inflate(context, R.layout.row_received_picture, null);
            case MESSAGE_TYPE_SENT_IMAGE:
                return View.inflate(context, R.layout.row_sent_picture, null);
        }
        return View.inflate(context, R.layout.row_received_message, null);
    }
}
